/* ----------------------------------------------------------------------------

	pax.window.js Copyright (C) 2005, 2007, 2008 Mikkel Bergmann, Pointful

	Licence
	-------
	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	See lgpl.txt for licence details

---------------------------------------------------------------------------- */

/*
	Script: pax.window
		This class creates and manages "windows" for various applications, such as dialogs and draggable windows.
		
	Author:
		Mikkel Bergmann, <http://www.pointful.com>

*/
var pax = pax || {};
pax.window = pax.window || {};
/*	Property: window.dict
	Window dictionary for tracking windows	*/
pax.window.dict = {};


/*	Method: pax.window.register
	Private method: Registers a window with the window manager
	
	Parameters:
		id - DOM pointer to the window element
*/
pax.window.register = function( id ) {
	pax.window.dict[id] = document.getElementById( id );
};


/*	Method: pax.window.get
	Private method: Returns a window from the window manager
	
	Parameters:
		id - DOM pointer to the window element
*/
pax.window.get = function( id ) {
	return pax.window.dict[id];
};


/*	Method: pax.window.deRegister
	Private method: Each window is removed from the manager, once it is hidden or destroyed
	
	Parameters:
		id - DOM pointer to the window element
*/
pax.window.deRegister = function( id ) {
	delete pax.window.dict[id];
};


/*	Method: pax.window.hideAll
	Hides all windows.
	
	Example:
		(start code)
			<span id='pax.window.hideAll.example' style='position:absolute;left:200px;top:200px;'>X</span>
			[:.
				var pos = pax.util.getPosition( pax.$('pax.window.hideAll.example') );
				pax.box.show( 'myWindow1', '^ X marks the spot!', 'hintMessage', pos.x, pos.y + pos.height + 5 );
				pax.box.show( 'myWindow2', '<- X marks the spot!', 'hintMessage', pos.x + pos.width + 5, pos.y );
				pax.box.show( 'myWindow3', 'X marks the spot! ->', 'hintMessage', pos.x - 135, pos.y );
				
				exampleCleanup = function() { 
					alert('About to hide all windows');
					pax.window.hideAll(); 
					alert('All windows hidden');
				};
			:]
		(end)
		This hides all windows in the window manager, you will note that it is run in the cleanup phase, with alerts to show you what is happening.
*/
pax.window.hideAll = function() {
	for( win in pax.window.dict )pax.box.hide( win );
};

/*	Method: pax.window.add
	Adds a window widget, and registeres it with the window manager
	
	Example:
		(start code)
			<select>
				<option>Drag a window over this in IE!</option>
			</select>
			[:.
				pax.window.add( pax.$('exampleDiv'), 'wid1', 'The window1 title', 'The content of the window', 50, 15 );
				pax.window.add( pax.$('exampleDiv'), 'wid2', 'The window2 title', 'The content of the window', 150, 250 );
				pax.window.add( pax.$('exampleDiv'), 'wid3', 'The window3 title', 'The content of the window', 250, 350 );
				pax.box.show( 'showBoxID', 'message', 'hintMessage', 10, 20, pax.$('exampleDiv') );
			:]
		(end)

*/
pax.window.add = function( target, id, title, content, x, y, args ) {
	//	TODO: Add preloading of the CSS images used in the window...
	
	//	The window template
	var myWindowTemplate = "" +
		"<div id='[:=id:]WindowTitle' class='TitleBar'> 								"+
		"	<span id='[:=id:]WindowCloseButton' class='Button Close'>x</span> 			"+
		"	<span id='[:=id:]WindowResizeButton' class='Button Resize'>=</span> 		"+
		"	<span id='[:=id:]WindowMinimizeButton' class='Button Minimize'>-</span> 	"+
		"	<span id='[:=id:]WindowTitleText' class='Title'>[:=title:]</span> 			"+
		"</div> 																		"+
		"<div class='Middle'> 															"+
		"	<div id='[:=id:]WindowContent' class='Content'>[:=content:]</div> 			"+
		"</div> 																		"+
	"";
	
	var myWindowStatusBarTemplate = "" +
		"<div id='[:=id:]Status' class='Status'> 										"+
		"	<span id='[:=id:]StatusButton' class='StatusButton'>/</span> 				"+		 
		"	<span id='[:=id:]StatusText' class='StatusText'>[:='statusText':]</span>	"+
		"</div> 																		"+
	"";

	args = args || {};
	args.handle = pax.$( id + 'WindowTitle');
	args.resetZIndex = false;
	args.showStatus = ( typeof( args.showStatus ) != 'undefined' )? args.showStatus: false;
	myWindowTemplate += myWindowStatusBarTemplate;

	var data = {
		id: id,
		title: title,
		content: content
	};

	//	Parse the template, and display it
	var myBox = pax.box.show( id, pax.template.parse( myWindowTemplate, data ).html, 'pWin', x, y, target );
	pax.util.setPosition( myBox, { x: 600 } );
	
	// Create a function if we're using IE, to move the iFrame.
	if( pax.isIe ) {
		args.callMove = function( args ) {
			pax.window.setPosition( args.args.element, args.x, args.y );
		};
		//	Method to remove the iframe reference, when the window is dropped.
		args.callBack = function( args ) {
			args.args.element.iFrame = null;
		};
	}

	pax.fx.drag( myBox, args );
	args.x = x;
	args.y = y;
	pax.util.setPosition( myBox, args );
	
	return pax.$(id);
};